package com.example.skytakeoutserver.config

import com.example.skytakeoutserver.handler.WebSocketHandler
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.HandlerMapping
import org.springframework.web.reactive.config.EnableWebFlux
import org.springframework.web.reactive.config.WebFluxConfigurer
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping

/**
 * Web socket 配置
 */
@Configuration
@EnableWebFlux
class WebSocketConfig(
    val webSocketHandler: WebSocketHandler
) : WebFluxConfigurer {
    /**
     *
     */
    @Bean
    fun webSocketHandlerMapping(): HandlerMapping {
        val mapping = SimpleUrlHandlerMapping()
        mapping.urlMap = mapOf("/ws/{sid}" to webSocketHandler)
        // 需要保证WebSocketHandler的优先级高于其他handler
        mapping.order = -1
        return mapping
    }
}